package com.xinghai.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinghai.common.annotation.DataScope;
import com.xinghai.common.utils.DateUtils;
import com.xinghai.system.domain.XhCarInfo;
import com.xinghai.system.domain.XhCarTypeInfo;
import com.xinghai.system.mapper.XhCarTypeInfoMapper;
import com.xinghai.system.service.IXhCarInfoService;
import com.xinghai.system.service.IXhCarTypeInfoService;
import com.xinghai.system.util.XHConstant;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.xinghai.common.utils.SecurityUtils.getDeptId;

/**
 * 车型信息Service业务层处理
 * 
 * @author xinghai
 * @date 2024-07-23
 */
@Service
public class XhCarTypeInfoServiceImpl extends ServiceImpl<XhCarTypeInfoMapper, XhCarTypeInfo> implements IXhCarTypeInfoService
{
    @Autowired
    private XhCarTypeInfoMapper xhCarTypeInfoMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Autowired
    private IXhCarInfoService xhCarInfoService;

    /**
     * 查询车型信息
     * 
     * @param id 车型信息主键
     * @return 车型信息
     */
    @Override
    public XhCarTypeInfo getInfo(Long id)
    {
        return xhCarTypeInfoMapper.getInfo(id);
    }

    /**
     * 查询车型信息列表
     * 
     * @param xhCarTypeInfo 车型信息
     * @return 车型信息
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<XhCarTypeInfo> list(XhCarTypeInfo xhCarTypeInfo)
    {
        return xhCarTypeInfoMapper.list(xhCarTypeInfo);
    }

    /**
     * 新增车型信息
     * 
     * @param xhCarTypeInfo 车型信息
     * @return 结果
     */
    @Override
    public int insert(XhCarTypeInfo xhCarTypeInfo) throws Exception {
        xhCarTypeInfo.setDeptId(getDeptId());
        if (this.lambdaQuery().eq(XhCarTypeInfo::getInfo,xhCarTypeInfo.getInfo())
                .eq(XhCarTypeInfo::getDeptId,xhCarTypeInfo.getDeptId())
                .eq(XhCarTypeInfo::getDelFlag,XHConstant.DEL_FLAG_NORMAL).count() > 0){
            throw new Exception("当前车型名称已经存在，请勿重复录入");
        }
        xhCarTypeInfo.setCreateTime(DateUtils.getNowDate());
        return xhCarTypeInfoMapper.insert(xhCarTypeInfo);
    }
    /**
     * 批量新增车型信息
     *
     * @param xhCarTypeInfoList 车型信息
     * @return 结果
     */
    @Override
    public int insertBatch(List<XhCarTypeInfo> xhCarTypeInfoList)
    {
        return xhCarTypeInfoMapper.insertBatch(xhCarTypeInfoList);
    }
    /**
     * 修改车型信息
     * 
     * @param xhCarTypeInfo 车型信息
     * @return 结果
     */
    @Override
    public int update(XhCarTypeInfo xhCarTypeInfo) throws Exception {
        if (this.lambdaQuery().ne(XhCarTypeInfo::getId,xhCarTypeInfo.getId())
                .eq(XhCarTypeInfo::getInfo,xhCarTypeInfo.getInfo())
                .eq(XhCarTypeInfo::getDeptId,xhCarTypeInfo.getDeptId())
                .eq(XhCarTypeInfo::getDelFlag,XHConstant.DEL_FLAG_NORMAL).count() > 0){
            throw new Exception("当前车型名称已经存在，请勿重复录入");
        }
        xhCarTypeInfo.setUpdateTime(DateUtils.getNowDate());
        return xhCarTypeInfoMapper.update(xhCarTypeInfo);
    }

    /**
     * 批量删除车型信息
     * 
     * @param ids 需要删除的车型信息主键
     * @return 结果
     */
    @Override
    public int deleteBatch(Long[] ids) throws Exception {
        if (ids.length == 1){
            //单一删除
            this.delete(ids[0]);
        }
        int i = 0;
        for (Long id : ids){
            //校验是否存在关联车辆信息
            if (xhCarInfoService.lambdaQuery().eq(XhCarInfo::getCarType,id).eq(XhCarInfo::getDelFlag, XHConstant.DEL_FLAG_NORMAL).count() > 0){
                continue;
            }
            xhCarTypeInfoMapper.delete(id);
            i++;
        }
        return i;
    }

    /**
     * 删除车型信息信息
     * 
     * @param id 车型信息主键
     * @return 结果
     */
    @Override
    public int delete(Long id) throws Exception {
        //校验是否存在关联车辆信息
        if (xhCarInfoService.lambdaQuery().eq(XhCarInfo::getCarType,id).eq(XhCarInfo::getDelFlag, XHConstant.DEL_FLAG_NORMAL).count() > 0){
            throw new Exception("当前车型已被车辆关联，不允许删除");
        }
        return xhCarTypeInfoMapper.delete(id);
    }

    /**
     * 导入车型信息信息
     *
     * @param list 车型信息信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<XhCarTypeInfo> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            XhCarTypeInfoMapper xhCarTypeInfoMapper = sqlSession.getMapper(XhCarTypeInfoMapper.class);
            list.forEach(item -> {
                item.setDeptId(getDeptId());
                if (this.lambdaQuery().eq(XhCarTypeInfo::getInfo,item.getInfo())
                        .eq(XhCarTypeInfo::getDeptId,item.getDeptId())
                        .eq(XhCarTypeInfo::getDelFlag,XHConstant.DEL_FLAG_NORMAL).count() == 0) {
                    xhCarTypeInfoMapper.insert(item);
                }
            });
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }

    @Override
    @DataScope(deptAlias = "d")
    public List<XhCarTypeInfo> carTypeList(XhCarTypeInfo xhCarTypeInfo) {
        return xhCarTypeInfoMapper.carTypeList(xhCarTypeInfo);
    }
}
